﻿using Sundry.IDAL;
using Sundry.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sundry.DAL
{
    public class AdminMenuRepository : BaseRepository<AdminMenu>, IAdminMenuRepository
    {
        /// <summary>
        /// 获取管理员显示菜单列表
        /// </summary>
        /// <typeparam name="S">排序类型</typeparam>
        /// <param name="whereLamda">查询条件</param>
        /// <param name="orderLamda">首次排序条件</param>
        /// <param name="thOrderLamda">再次排序条件</param>
        /// <param name="selector">查询结果字段</param>
        /// <returns>管理员显示菜单列表</returns>
        public IQueryable<F> GetAdminMenu<S, F>(Expression<Func<AdminMenu, bool>> whereLamda, Expression<Func<AdminMenu, S>> orderLamda, Expression<Func<AdminMenu, S>> thOrderLamda, Expression<Func<AdminMenu, F>> selector)
        {
            IQueryable<AdminMenu> adminQueryable = LoadEntities(whereLamda);
            IOrderedQueryable<AdminMenu> adminOrderedQueryable =Order(adminQueryable, orderLamda);
            adminOrderedQueryable = ThenOrder(adminOrderedQueryable, thOrderLamda);
            return adminOrderedQueryable.Select(selector);
        }
        /// <summary>
        /// 获取管理员显示菜单列表
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="F"></typeparam>
        /// <param name="whereLamdaList">查询条件</param>
        /// <param name="orderLamdaDic">排序列表</param>
        /// <param name="startNum">起始数字</param>
        /// <param name="pageSize">长度</param>
        /// <param name="rowCount">总数目</param>
        /// <returns></returns>
        public IQueryable<dynamic> GetAdminMenu<S>(IEnumerable<Expression<Func<AdminMenu, bool>>> whereLamdaList, Dictionary<Expression<Func<AdminMenu, S>>, bool> orderLamdaDic, int startNum, int pageSize, out int rowCount) {

            var result = from p in db.Set<AdminMenu>()
                         select p;
            if (whereLamdaList != null) {
                foreach (var item in whereLamdaList) {
                    result = result.Where(item);
                }
            }
            //排序
            if (orderLamdaDic != null&&orderLamdaDic.Count>0) {
                for (int i = 0; i < orderLamdaDic.Count; i++) {
                    if (i == 0) {
                        if (orderLamdaDic.Values.ElementAt(0) == true) {
                            result = result.OrderByDescending(orderLamdaDic.Keys.ElementAt(0));
                        }
                        else {
                            result = result.OrderBy(orderLamdaDic.Keys.ElementAt(0));
                        }
                    }
                    else {
                        if (orderLamdaDic.Values.ElementAt(i) == true) {
                            result = (result as IOrderedQueryable<AdminMenu>).ThenByDescending(orderLamdaDic.Keys.ElementAt(i));
                        }
                        else {
                            result = (result as IOrderedQueryable<AdminMenu>).ThenBy(orderLamdaDic.Keys.ElementAt(i));
                        }
                    }
                }
            }
            rowCount = result.Count();
            result = result.Skip(startNum).Take(pageSize);
            return result;
        }
    }
}
